package com.braveman.im.ui.activity.message;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.os.PersistableBundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.braveman.im.R;
import com.braveman.im.utils.UtilsStyle;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.dialog.LoadingDialog4Pay;
import com.ht.baselibs.utils.ActivityUtils;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.vondear.rxtools.RxBarTool;


import static com.google.zxing.integration.android.IntentIntegrator.REQUEST_CODE;

/**
 * D
 * 主扫
 */

public class QrCodeActivity extends BaseActivity implements DecoratedBarcodeView.TorchListener { // 实现相关接口
    // 添加一个按钮用来控制闪光灯，同时添加两个按钮表示其他功能，先用Toast表示
    DecoratedBarcodeView mDBV;
    private CaptureManager captureManager;
    private TextView mQrcodeHelpTextView;
    private PopupWindow popupWindow;
    private static final int MSG_WHAT_NO_SUCCESS = 0;
    private String money;


    private LoadingDialog4Pay loadingDialog4Pay;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("扫码加好友");
    }


    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_qr_code;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadingDialog4Pay = loadingDialog4Pay.newInstance(QrCodeActivity.this, false);
        //重要代码，初始化捕获
        captureManager = new CaptureManager(this, mDBV);
        captureManager.initializeFromIntent(getIntent(), savedInstanceState);
        captureManager.decode();
        captureManager.setAutoColseQR(false);

        captureManager.setOnSuccessResultListener(new CaptureManager.OnSuccessResultListener() {

            @Override
            public void getIntent(Intent intent) {
                loadingDialog4Pay.show();
                IntentResult intentResult = IntentIntegrator.parseActivityResult(REQUEST_CODE, Activity.RESULT_OK, intent);
                if (intentResult != null) {
                    String authCode = intentResult.getContents();
//                    Bundle[{SCAN_RESULT=134931474357148477, SCAN_RESULT_BYTES=[B@4373b658, SCAN_RESULT_FORMAT=QR_CODE, SCAN_RESULT_ERROR_CORRECTION_LEVEL=H}]
                    ActivityUtils.startActivity(QrCodeActivity.this, GroupInfoActivity.class);
                }
            }


        });


    }

    @Override
    protected void initChildView() {
        mDBV = (DecoratedBarcodeView) findViewById(R.id.dbv_custom);
        mQrcodeHelpTextView = (TextView) findViewById(R.id.tv_qrcode_zhusao_help);
        TextView mMoneyTextView = (TextView) findViewById(R.id.tv_qrcode_zhusao);
        money = getIntent().getStringExtra("money");
        mMoneyTextView.setText("¥ " + money);
        mDBV.setTorchListener(this);
    }

    @Override
    protected void initChildData() {

    }

    @Override
    protected void setChildViewListener() {
        mQrcodeHelpTextView.setOnClickListener(this);
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        captureManager.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    // torch 手电筒
    @Override
    public void onTorchOn() {
        Toast.makeText(this, "torch on", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onTorchOff() {
        Toast.makeText(this, "torch off", Toast.LENGTH_LONG).show();
    }

    // 判断是否有闪光灯功能

    private boolean hasFlash() {
        return getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    @Override
    protected void onPause() {
        super.onPause();
        captureManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        captureManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        captureManager.onDestroy();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_qrcode_zhusao_help:
                // TODO: 2017/6/22 跳转到帮助页面
                showPopupWindow();
                break;
            case R.id.iv_pop_accord_cord_help:
                popupWindow.dismiss();
                break;
        }
    }

    private void showPopupWindow() {
        View view = View.inflate(this, R.layout.pop_accord_cord_help,
                null);
        popupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setAnimationStyle(R.style.popupwindow_anim);
        view.findViewById(R.id.iv_pop_accord_cord_help).setOnClickListener(this);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(mQrcodeHelpTextView, Gravity.NO_GRAVITY, 0, 0);
    }

    @Override
    protected void handleMessage(BaseActivity context, Message msg) {
        switch (msg.what) {
            case MSG_WHAT_NO_SUCCESS:
                break;

        }

    }

}
